package com.cn.ccp.action;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cn.ccp.bean.CourseBean;
import com.cn.ccp.bean.CourseContentBean;
import com.cn.ccp.bean.FacultyBean;
import com.cn.ccp.bean.FilePathBean;
import com.cn.ccp.util.CCPContants;

public class PublicAction extends BaseAction {
	
	
	public String loginPageImage ;//登录界面图片
	public String uploadImageType = "1" ;//上传图片类型 1：登录界面  ； 2：标题
	
	//图片格式
	public String imageLayout ;
	//课程上传文件类型
	public String uploadFileLayout  ;
	//课程上传视频类型
	public String uploadVideoLayout ;
	
	private SelectItem[] facultySelectItem ;
	
	private SelectItem[] couresCountSelectItem ;
	
	private SelectItem[] couresSelectItem ;
	
	
	public void submitLoginPageImage(){
		if(loginPageImage == null || loginPageImage.equals(""))
			return ;
		String savePath = CCPContants.SYS_PATH+"files"+File.separator +loginPageImage;
		
		FilePathBean filePathBean = getFilePathService().getFlagBean();
		if(filePathBean != null){
			savePath = filePathBean.getPath()+loginPageImage;
		}
		String loginPagePath = CCPContants.SYS_PATH+"images/login.gif" ;
		if(uploadImageType.equals("1")){
			loginPagePath = CCPContants.SYS_PATH+"images/login.gif" ;
		}else if(uploadImageType.equals("2")){
			loginPagePath = CCPContants.SYS_PATH+"images/logo.png" ;
		}else if(uploadImageType.equals("3")){
			loginPagePath = CCPContants.SYS_PATH+"images/logo_main_page.png" ;
		} else{
			System.err.println("unkone upload image type "+ uploadImageType);
		}
		CCPContants.copyFile(savePath, loginPagePath);
		
		System.out.println("---submitLoginPageImage"+savePath);
		FacesContext context = FacesContext.getCurrentInstance();
		context.addMessage("formid", new FacesMessage(getMessages("file_upload_success")));
	}
	
	public String goBack(){
		return CCPContants.getValue(CCPContants.GO_BAKC);
	}
	

	public SelectItem[] getFacultySelectItem() {
		if(facultySelectItem == null || facultySelectItem.length == 0){
			List<FacultyBean> list = getFacultyService().getAllFaculty();
			facultySelectItem = new SelectItem[list.size()];
			for(int i = 0 ; i < list.size() ; i++)
				facultySelectItem[i] = new SelectItem(""+list.get(i).getId() , list.get(i).getFacultyName());
		}
		return facultySelectItem;
	}

	public void setFacultySelectItem(SelectItem[] facultySelectItem) {
		this.facultySelectItem = facultySelectItem;
	}

	public SelectItem[] getCouresCountSelectItem() {
		if(couresCountSelectItem == null){
			couresCountSelectItem = new SelectItem[100];
			for (int i = 1; i < 101; i++) 
				couresCountSelectItem[i-1] = new SelectItem(""+i,""+i);
		}
		return couresCountSelectItem;
	}

	public void setCouresCountSelectItem(SelectItem[] couresCountSelectItem) {
		this.couresCountSelectItem = couresCountSelectItem;
	}

	public String getImageLayout() {
		return CCPContants.imageLayout;
	}

	public void setImageLayout(String imageLayout) {
		this.imageLayout = imageLayout;
	}

	public String getUploadFileLayout() {
		return CCPContants.uploadFileLayout;
	}

	public void setUploadFileLayout(String uploadFileLayout) {
		this.uploadFileLayout = uploadFileLayout;
	}

	public String getUploadVideoLayout() {
		return CCPContants.uploadVideoLayout;
	}

	public void setUploadVideoLayout(String uploadVideoLayout) {
		this.uploadVideoLayout = uploadVideoLayout;
	}


	public String getLoginPageImage() {
		return loginPageImage;
	}


	public void setLoginPageImage(String loginPageImage) {
		this.loginPageImage = loginPageImage;
	}

	public String getUploadImageType() {
		return uploadImageType;
	}

	public void setUploadImageType(String uploadImageType) {
		this.uploadImageType = uploadImageType;
	}

	public SelectItem[] getCouresSelectItem() {
		if(couresSelectItem == null){
			List<CourseBean> list = getCourseService().getAllCourse();
			couresSelectItem = new SelectItem[list.size()];
			for (int i = 0; i < list.size(); i++) {
				couresSelectItem[i] = new SelectItem(""+list.get(i).getId() , list.get(i).getCourse_name());
			}
		}
		return couresSelectItem;
	}

	public void setCouresSelectItem(SelectItem[] couresSelectItem) {
		this.couresSelectItem = couresSelectItem;
	}
	
	
	
	public String createCourseContentHtmlText(List<CourseContentBean> zhangjieList ,List<CourseContentBean> xiaojieList){
		StringBuffer buf = new StringBuffer();
		
		
		for(CourseContentBean zjBean : zhangjieList){
			String s = "<div class='accordion-group'>"+
                          "<div class='accordion-heading'>"+
                            "<a class='accordion-toggle' data-toggle='collapse' data-parent='#accordion1' href='#collapseOne'>"+
                             " 第一章 概述"+
                            "</a>"+
                          "</div>"+
                          "<div id='collapseOne' class='accordion-body'>"+	  
                            "<div class='accordion-inner'>"+
                                "<ol class='unstyled'>"+
									"<li><i class='icon-eye-open'></i>B. 文档名称[Word]</li>"+
                                    "<li><i class='icon-play'></i>C. 文档名称[视频]</li>"+
                                    "<li>"+
                                    	"<a class='accordion-toggle' data-toggle='collapse' data-parent='#collapseOne' href='#collapseTwoaa'>"+
                                              "第二课 可行性研究"+
                                            "</a>"+
                                          "<div id='collapseTwoaa' class='accordion-body'>"+
                                               "<ol class='unstyled'>"+
                                                    "<li><i class='icon-eye-open'></i>A. 文档名称[PDF]</li>"+
                                                    "<li><i class='icon-eye-open'></i>B. 文档名称[Word]</li>"+
                                                    "<li><i class='icon-play'></i>C. 文档名称[视频]</li>"+
                                                    "<li><i class='icon-pencil'></i>D. 课后练习</li>"+
                                                "</ol>"+
                                          "</div>"+
                                        "</li>"+
                                    "<li>"+
										"<a class='accordion-toggle' data-toggle='collapse' data-parent='#collapseOne' href='#collapseTwoa'>"+
                                              "第二课 可行性研究"+
                                            "</a>"+
                                          "<div id='collapseTwoa' class='accordion-body'>"+
                                           "<ol class='unstyled'>"+
                                                    "<li><i class='icon-eye-open'></i>A. 文档名称[PDF]</li>"+
                                                    "<li><i class='icon-eye-open'></i>B. 文档名称[Word]</li>"+
                                                    "<li><i class='icon-play'></i>C. 文档名称[视频]</li>"+
                                                    "<li><i class='icon-pencil'></i>D. 课后练习</li>"+
                                                "</ol>"+
                                            "</div>"+
                                      "</li>"+
                                "</ol>"+
                            "</div>"+
                          "</div>"+
                        "</div>";
		}
		
		return buf.toString() ;
	}
	
}
